define(['../../../carrys/md5'],function() {
    return ['$scope','$location', '$interval','$timeout','bizProxy', function($scope,$location,$interval,$timeout,bizProxy) {
        $scope.$emit("top-show",'true');//顶部显示
        $scope.$emit('menuTypeLight','mine'); 	//控制底部menu高亮
        $scope.$emit('bub-menu','true'); 	//隐藏底部menu
        $scope.$emit('flag-tag','5');//显示底部文字
    	
    	//默认值
    	$scope.mobileNo = transData.mobileNo;
    	$scope.authCode = "";
    	$scope.password = "";
    	$scope.yzmLight = false;
    	
    	$scope.showSend = true;
    	$scope.showTime = false;
    	$scope.isactive = false;
    	$scope.countdown = 60;
    	var tempCheckNo = "";var timeOutId;

    	//监听手机号是否要高亮
    	$scope.$watch('mobileNo', function() {
    		if(($scope.mobileNo).length > 0){
    			if(($scope.mobileNo).length == 11){
        			$scope.yzmLight = true;
        		}else{
        			$scope.yzmLight = false;
        		}
        		changeStyle();
    		}
		});
    	
    	$scope.$watch('authCode', function() {
    		if(($scope.authCode + '').length > 0){
    			changeStyle();
    		}
		});
    	
    	//点击获取验证码
    	$scope.getAuthCode = function(){
    		if(! _SYT_util.isValidNo($scope.mobileNo)){
                $scope.$emit("promptAlertMsg","手机号无效");
                $scope.$emit("showCollect",true);
                $timeout(function(){
                    $scope.$emit("showCollect",false);
                },transData.promptTime);
    			return;
    		}
    		$scope.showSend = false;
        	$scope.showTime = true;
    		getAuthCode();
    	}
    	
    	//去下一步
    	$scope.goNext = function(){
			if(!($scope.authCode+"")){
                $scope.$emit("promptAlertMsg","请输入验证码");
                $scope.$emit("showCollect",true);
                $timeout(function(){
                    $scope.$emit("showCollect",false);
                },transData.promptTime);
				return;
			}
    		//验证验证码
    		if(tempCheckNo == hex_md5($scope.authCode+"")){
    			transData.mobileNo = $scope.mobileNo;
        		transData.mobileCheckNo = $scope.authCode+"";
        		startLogin();
    		}else{
                $scope.$emit("promptAlertMsg","验证码错误");
                $scope.$emit("showCollect",true);
                $timeout(function(){
                    $scope.$emit("showCollect",false);
                },transData.promptTime);
    			 transData.mobileCheckNo = "";
				 $scope.authCode = "";
    		}
			if(!($scope.password+"")){
                $scope.$emit("promptAlertMsg","请输入密码");
                $scope.$emit("showCollect",true);
                $timeout(function(){
                    $scope.$emit("showCollect",false);
                },transData.promptTime);
				return;
			}
    	}

    	//点击去用户协议
    	$scope.mZprotocol = function(){
            $scope.$emit("goNext","/fireFlyprotocol");
    	}

    	function startLogin(){
       		var headerInfo = {},bizInfo = {};
           	
           	//用户登录
           	headerInfo.pageNum = '';
           	headerInfo.pageSize = '';
           	headerInfo.sessionId = '';

            if(_YHC_isWEIXIN.isWeiXin()){
                console.log("isWeiXin"+localStorage.getItem("sessionId"));
                console.log("开始微信登陆绑定");
                bizInfo = transData.authInfo;
            }else{
                console.log("不是微信环境");
                bizInfo.loginType = "reg";
                bizInfo.regChannel = "1";
            }

			bizInfo.mobileNo = $scope.mobileNo;
			bizInfo.password = $scope.password;
			bizInfo.mobileCheckNo = $scope.authCode+"";
			console.log(bizInfo)
           	$scope.$emit("loadingChange",true);
           	bizProxy.queryProxyBizData("userLogin", headerInfo,bizInfo,
    	   		function(tempData){
                    console.log(tempData.body);
       				$scope.$emit("loadingChange",false);
    	   			if(tempData.sessionId != "" && tempData.sessionId != null){
    	   				$timeout(function(){
                            localStorage.setItem("fireFlySessionId",tempData.sessionId+"");
                            transData.pwd1 = "",transData.pwd2 = "",transData.mobileCheckNo = "";
                            var body = JSON.parse(tempData.body);
                            transData.mobileNo = body.mobileNo;
                            transData.sessionId = tempData.sessionId;
                            transData.userId = body.userId;
                            resultData.loginResultData = body;
                            queryUserExt()
    	   				},0);
    	   			}

    	   		},function(data){
    	   			console.log(data);
    	   			$scope.$emit("loadingChange",false);
                    $scope.$emit("promptAlertMsg",data.errorMsg);
                    $scope.$emit("showCollect",true);
                    $timeout(function(){
                        $scope.$emit("showCollect",false);
                    },transData.promptTime);
    	   		}
    	   	);
       		
       	}

        //查询用户扩展信息
        function queryUserExt(){
            var headerInfo = {},bizInfo = {};
            headerInfo.pageNum = '';
            headerInfo.pageSize = '';
            headerInfo.sessionId = transData.sessionId;
            $scope.$emit("loadingChange",true);
            bizProxy.queryProxyBizData("queryUserExt",headerInfo,bizInfo,
                function(tempData){//SUCCESS
                    console.log("查询用户信息=",tempData);
                    $scope.$emit("loadingChange",false);
                    $timeout(function(){
                        transData.userType=tempData.userType;
                        resultData.loginUserData=tempData;
                        $scope.$emit("goNext","/regSuccess");
                    },100);
                },function(data){
                    console.log(data)
                    if(data != ""){//FAIL
                        $scope.$emit("loadingChange",false);
                        if(data.errorCode == ERROR_CODE.noLogin){
                            FIREFLY("", data.errorMsg,"",function(){
                                //调用退出登录
                                transData.mobileNo = "";
                                transData.password = "";
                                transData.sessionId = "";
                                transData.userType = "";
                                resultData.loginUserData = "";
                                $timeout(function(){
                                    if(transData.userType === '1'){
                                        $scope.$emit("goNext","/mineLs");
                                    }else{
                                        $scope.$emit("goNext","/minePt");
                                    }
                                },0);
                            });
                        }else{
                            $scope.$emit("promptAlertMsg",data.errorMsg);
                            $scope.$emit("showCollect",true);
                            $timeout(function(){
                                $scope.$emit("showCollect",false);
                            },transData.promptTime);
                        }
                    }else{
                        $scope.$emit("loadingChange",false);
                        $scope.$emit("promptAlertMsg","连接超时");
                        $scope.$emit("showCollect",true);
                        $timeout(function(){
                            $scope.$emit("showCollect",false);
                        },transData.promptTime);
                    }
                });
        }

    	//请求代理 获取验证码
    	function getAuthCode(){
    		startCountDown();
    		var headerInfo = {},bizInfo = {};
           	headerInfo.pageNum = '';
           	headerInfo.pageSize = '';
           	headerInfo.sessionId =transData.sessionId;
           	
           	bizInfo.mobileNo = $scope.mobileNo;
           	bizInfo.smsKind = "checkNo";
           	bizProxy.queryProxyBizData("sendCheckNo", headerInfo,bizInfo,
       				function(tempData){
       					tempCheckNo = tempData.body;
       				},
       				function(data){
                        $scope.$emit("promptAlertMsg",data.errorMsg);
                        $scope.$emit("showCollect",true);
                        $timeout(function(){
                            $scope.$emit("showCollect",false);
                        },transData.promptTime);
       				}
       		);
    	}
    	
    	//切换样式
    	function changeStyle(){
    		if(($scope.mobileNo).length == 11 && ($scope.authCode + '').length == 6){
				$scope.isactive = true;
			}else{
				$scope.isactive = false;
			}
    	}
    	
    	//开始倒计时
        function startCountDown(){
     	   timeOutId = $interval(function(){
                if (--$scope.countdown == 0){
             	   stopCountDown();
             	   $scope.countdown = 60;
             	   $scope.showSend = true;
             	   $scope.showTime = false;
                }
            }, 1000);
        }
        
        //停止倒计时
        function stopCountDown() {
     	   $interval.cancel(timeOutId);
        }


		//=================================
		$scope.changeTypePwd=true;
		$scope.changeType=function(){
			$scope.changeTypePwd=!$scope.changeTypePwd;
			if($scope.changeTypePwd){
				document.querySelector('#register_password').type='password';
			}else {
				document.querySelector('#register_password').type='text'
			}
		};



      //当键盘被销毁的时候
        $scope.$on("$destroy", function() {
	      	stopCountDown();
	    });
    	$scope.$apply();
    }];
});